//
//  LoginViewController.m
//  fitness-iOS
//
//  Created by 卢仕彤 on 2017/11/9.
//  Copyright © 2017年 卢仕彤. All rights reserved.
//

#import "LoginViewController.h"
#import "LoginTextField.h"
#import "UIColor+Expand.h"
#import "FFNetworkHelper.h"
#import "FFCache.h"
#import "RegisteViewController.h"
#import "ForgetViewController.h"


@interface LoginViewController () <UITextFieldDelegate>

@property (nonatomic, copy) UIImageView *logoImageView;
@property (nonatomic, strong) UIView *whiteBgView;
@property (nonatomic, strong) UILabel *phoneLabel;
@property (nonatomic, strong) UILabel *passwordLabel;
@property (nonatomic, strong) LoginTextField *phoneTextField;
@property (nonatomic, strong) LoginTextField *passwordTextField;
@property (nonatomic, strong) UIButton *loginButton;

@property (nonatomic, strong) UIButton *forgetButton;
@property (nonatomic, strong) UIButton *registButton;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setRightNavigationRetunItem];
    self.title = @"登录";
    [self configLoginViewC];
}

- (void)configLoginViewC {
    [self.view addSubview:self.whiteBgView];
    [self.whiteBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];

    [self.whiteBgView addSubview:self.logoImageView];
    [self.logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.whiteBgView.centerX);
        make.size.equalTo(CGSizeMake(80, 80));
        make.top.equalTo(self.whiteBgView.top).offset(80);
    }];
    
    [self.whiteBgView addSubview:self.phoneLabel];
    [self.phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.whiteBgView.mas_left).offset(30);
        make.centerX.equalTo(self.logoImageView.centerX);
        make.top.equalTo(self.logoImageView.mas_bottom).offset(30);
    }];
    
    [self.whiteBgView addSubview:self.phoneTextField];
    [self.phoneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.phoneLabel.mas_left);
        make.right.equalTo(self.whiteBgView.mas_right).offset(-30);
        make.top.equalTo(self.phoneLabel.mas_bottom).offset(10);
    }];
    
    [self.whiteBgView addSubview:self.passwordLabel];
    [self.passwordLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.whiteBgView.mas_left).offset(30);
        make.centerX.equalTo(self.logoImageView.centerX);
        make.top.equalTo(self.phoneLabel.mas_bottom).offset(80);
    }];
    
    [self.whiteBgView addSubview:self.passwordTextField];
    [self.passwordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.passwordLabel.mas_left);
        make.right.equalTo(self.whiteBgView.mas_right).offset(-30);
        make.top.equalTo(self.passwordLabel.mas_bottom).offset(10);
    }];
    
    [self.whiteBgView addSubview:self.loginButton];
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.whiteBgView.mas_left).offset(30);
        make.right.equalTo(self.whiteBgView.mas_right).offset(-30);
        make.centerX.equalTo(self.whiteBgView.mas_centerX);
        make.top.equalTo(self.passwordTextField.mas_bottom).offset(30);
    }];
    
    [self.whiteBgView addSubview:self.forgetButton];
    [self.forgetButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.whiteBgView.mas_left).offset(30);
        make.bottom.equalTo(self.whiteBgView.mas_bottom).offset(-35);
    }];
    
    [self.whiteBgView addSubview:self.registButton];
    [self.registButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.whiteBgView.mas_right).offset(-30);
        make.bottom.equalTo(self.whiteBgView.mas_bottom).offset(-35);
    }];
}

#pragma mark - lazy init

- (UIImageView *)logoImageView {
    if (!_logoImageView) {
        _logoImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"fittness_log"]];
        _logoImageView.backgroundColor = [UIColor redColor];
    }
    return _logoImageView;
}

- (UIView *) whiteBgView {
    if (!_whiteBgView) {
        _whiteBgView = [[UIView alloc] init];
        _whiteBgView.backgroundColor = [UIColor whiteColor];
    }
    return _whiteBgView;
}

- (UILabel *)phoneLabel {
    if (!_phoneLabel) {
        _phoneLabel = [UILabel labelWithSystemFontSize:15 textColorHexString:@"#333333"];
        _phoneLabel.text = @"手机号";
    }
    return _phoneLabel;
}

- (UILabel *)passwordLabel {
    if (!_passwordLabel) {
        _passwordLabel = [UILabel labelWithSystemFontSize:15 textColorHexString:@"#333333"];
        _passwordLabel.text = @"密码";
    }
    return _passwordLabel;
}

- (LoginTextField *)passwordTextField {
    if (!_passwordTextField) {
        _passwordTextField = [[LoginTextField alloc] init];
        _passwordTextField.delegate = self;
        _passwordTextField.leftViewMode = UITextFieldViewModeAlways;
        _passwordTextField.placeholder = @"请输入密码";
        _passwordTextField.font = [UIFont systemFontOfSize:16];
        _passwordTextField.textColor = [UIColor colorWithHexString:@"333333"];;
        _passwordTextField.returnKeyType = UIReturnKeyDone;
        _passwordTextField.secureTextEntry = YES;
        _passwordTextField.rightViewMode = UITextFieldViewModeAlways;
        [_passwordTextField setValue:[UIFont systemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
        [_passwordTextField setValue:[UIColor colorWithHexString:@"D8D8D8"] forKeyPath:@"_placeholderLabel.textColor"];
        _passwordTextField.keyboardType = UIKeyboardTypeDefault;
    }
    return _passwordTextField;
}

- (LoginTextField *) phoneTextField {
    
    if (!_phoneTextField) {
        _phoneTextField = [[LoginTextField alloc] init];
        _phoneTextField.delegate = self;
        _phoneTextField.leftViewMode = UITextFieldViewModeAlways;
        _phoneTextField.placeholder = @"请输入手机号";
        _phoneTextField.font = [UIFont systemFontOfSize:16];
        _phoneTextField.textColor = [UIColor colorWithHexString:@"333333"];;
        _phoneTextField.returnKeyType = UIReturnKeyDone;
        _phoneTextField.rightViewMode = UITextFieldViewModeAlways;
        [_phoneTextField setValue:[UIFont systemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
        [_phoneTextField setValue:[UIColor colorWithHexString:@"D8D8D8"] forKeyPath:@"_placeholderLabel.textColor"];
        _phoneTextField.keyboardType = UIKeyboardTypeDefault;
    }
    return _phoneTextField;
}

- (UIButton *)loginButton {
    if (!_loginButton) {
        _loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_loginButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [_loginButton addTarget:self action:@selector(didLoginClick) forControlEvents:UIControlEventTouchUpInside];
        [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
        _loginButton.titleLabel.font = [UIFont systemFontOfSize:16];
        _loginButton.layer.cornerRadius = 18;
        _loginButton.layer.borderWidth = 0.5;
        _loginButton.layer.borderColor = [UIColor colorWithHexString:@"414C50"].CGColor;
        _loginButton.layer.masksToBounds = YES;
    }
    return _loginButton;
}

- (UIButton *)forgetButton {
    if (!_forgetButton) {
        _forgetButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_forgetButton setTitleColor:[UIColor colorWithHexString:@"333333"] forState:UIControlStateNormal];
        [_forgetButton setTitle:@"验证码登录" forState:UIControlStateNormal];
        _forgetButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [_forgetButton addTarget:self action:@selector(registClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _forgetButton;
}

- (UIButton *)registButton {
    if (!_registButton) {
        _registButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_registButton setTitle:@"注册账号" forState:UIControlStateNormal];
        _registButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [_registButton setTitleColor:[UIColor colorWithHexString:@"333333"] forState:UIControlStateNormal];
        [_registButton addTarget:self action:@selector(registClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _registButton;
}


- (void)registClick {
    RegisteViewController *reVC = [[RegisteViewController alloc] init];
    [self.navigationController pushViewController:reVC animated:YES];
}

- (void)didLoginClick {
    
    NSLog(@"%@ %@",_phoneTextField.text,_passwordTextField.text);
    NSString *requestUrl = @"login/doLogin";
    
    NSDictionary *parmsDic = @{@"phone":_phoneTextField.text,
                               @"password":_passwordTextField.text,
                               @"code":@(123456)
                               };
    [FFNetworkHelper openLog];
    [FFNetworkHelper POST:requestUrl parameters:parmsDic success:^(id responseObject) {
        if ([responseObject[@"code"] integerValue] == 200) {
            [FFCache setToken:responseObject[@"data"][@"token"]];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self dismissViewControllerAnimated:YES completion:nil];
                
            });
        }
    } failure:^(NSError *error) {
        
    }];
    
}

- (void)setRightNavigationRetunItem {
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"忘记密码" style:UIBarButtonItemStylePlain target:self action:@selector(pushPassWord)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor colorWithHexString:@"333333"];
}
- (void)pushPassWord{
    ForgetViewController *fVC = [[ForgetViewController alloc] init];
    [self.navigationController pushViewController:fVC animated:YES];
    
}

#pragma -mark ------------------lazy init----------------------


@end
